/**
 * 下一步操作
 */
import React, {Component} from 'react';

import './index.less';

import {
    base,
    toast
} from 'nc-lightapp-front';
import CenterEffectContAction from "./functions/CenterEffectContAction";
import CenterDeleteContAction from "./functions/CenterDeleteContAction";
import {jump} from "../../main/common";

const {
    NCModal,
    NCButton,
    NCRadio
} = base;

const {
    Header: NCModalHeader,
    Body: NCModalBody,
    Footer: NCModalFooter
} = NCModal;

class NextOperation extends Component {
    constructor(props) {
        super(props);
        this.state = {
            json: props.json||{},
            selectedValue: 'into',
            extendtype: '1'
        }
    }

    componentWillReceiveProps(nextprops) {
        let nextjson = JSON.stringify(nextprops.json);
        let thisjson = JSON.stringify(this.props.json);
        if (nextjson !== thisjson) {
            this.setState({
                json: nextprops.json,
            })
        }
    }

    confirm = () => {
        if (!this.props.ctrtVOs && this.props.transactionModelId === 'extend') {
            let extendtype = this.state.extendtype;
            this.props.updateState({
                extendtype
            }, () => {
                this.props.showTransaction(extendtype);
                this.closeInfoModal();
            });
        } else {
            // 确定
            let selectedValue = this.state.selectedValue;
            this[selectedValue]();
        }
    };

    /**
     * 进入业务节点
     * @param pageUrl  /nccloud/resources/hrcm/contractmgt/change/main
     */
    into() {
        // 跳转到其他节点
        jump(this.props.vPks, this.props.hrorgobj, this.props.pageUrl);
        this.closeInfoModal();
    }

    /**
     * 生效
     */
    enable() {
        CenterEffectContAction(this.props.hrorgval, this.props.vPks).then(res => {
            toast({
                color: 'success',
                content: this.state.json['cm6011-000073']
            });//已生效！
            this.closeInfoModal();
            this.props.refresh();
        })
    }

    /**
     * 删除
     */
    del() {
        CenterDeleteContAction(this.props.hrorgval, this.props.vPks).then(res => {
            toast({
                color: 'success',
                content: this.state.json['cm6011-000074']
            });//已删除
            this.closeInfoModal();
            this.props.refresh();
        })
    }

    closeInfoModal = () => {
        this.props.updateState({
            nextOperationVisible: false
        });
        this.setState({
            selectedValue: 'into',
            extendtype: '1'
        })
    };

    /**
     * 选择下一步操作
     */
    handleChange = (value) => {
        this.setState({selectedValue: value});
    };

    handleExtend = (value) => {
        this.setState({extendtype: value});
    };

    render() {
        const {
            nextOperationVisible
        } = this.props;
        return (
            <NCModal
                backdrop="static"
                show={nextOperationVisible}
                size="sm"
                className="next-operation"
                onHide={this.closeInfoModal}
                // onEntered={this.showHandle}
            >
                <NCModalHeader
                    closeButton={true}
                >
                    {this.state.json['cm6011-000070']}{/* 国际化处理： 请选择下一步操作*/}
                </NCModalHeader>
                <NCModalBody>
                    <div className="next-operation">
                        <div className={'message'} dangerouslySetInnerHTML={{
                            __html: this.props.warnInf
                        }}>
                        </div>
                        {(this.props.transactionModelId === 'extend' && !this.props.ctrtVOs) ?
                            <div className={'radio-extend'}>
                                <NCRadio.NCRadioGroup
                                    name="extend"
                                    selectedValue={this.state.extendtype}
                                    onChange={this.handleExtend.bind(this)}>

                                    <NCRadio value="1">{this.state.json["cm6011-000097"]}</NCRadio>

                                    <NCRadio value="2">{this.state.json["cm6011-000098"]}</NCRadio>
                                </NCRadio.NCRadioGroup>
                            </div> : <div className={'radio-box'}>
                                <NCRadio.NCRadioGroup
                                    name="fruit"
                                    selectedValue={this.state.selectedValue}
                                    onChange={this.handleChange.bind(this)}>

                                    <NCRadio value="into">{this.state.json["cm6011-000072"]}</NCRadio>

                                    <NCRadio value="enable">{this.state.json["cm6011-000071"]}</NCRadio>

                                    <NCRadio value="del">{this.state.json["cm6011-000065"]}</NCRadio>

                                </NCRadio.NCRadioGroup>
                            </div>}

                    </div>
                </NCModalBody>
                <NCModalFooter>
                    <NCButton
                        colors="primary"
                        onClick={this.confirm.bind(this)}
                    >
                        {this.state.json['cm6011-000028']}{/* 国际化处理： 确认*/}
                    </NCButton>
                    <NCButton
                        colors="secondary"
                        onClick={this.closeInfoModal}
                    >
                        {this.state.json['cm6011-000054']}{/* 国际化处理： 取消*/}
                    </NCButton>
                </NCModalFooter>
            </NCModal>
        );
    }
}

export default NextOperation
